package com.gusha.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysCommentDao;
import com.gusha.model.InfoContent;
import com.gusha.model.SysComment;
import com.gusha.model.User;

@Service
@Transactional
public class SysCommentService {
	@Autowired
	private SysCommentDao sysCommentDao;
	
	
	 public Page<SysComment> findAll(Pageable pageable, SysComment sysComment) {
	        @SuppressWarnings("serial")
	        Page<SysComment> result = sysCommentDao.findAll(new Specification<SysComment>() {
	            @Override
	            public Predicate toPredicate(Root<SysComment> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
	                List<Predicate> list = new ArrayList<Predicate>();
	                if (sysComment.getId() != null) {
	                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysComment.getId()));
	                }
	                if (sysComment.getContentId() != null) {
	                    list.add(cb.equal(root.get("contentId").as(InfoContent.class),sysComment.getContentId()));
	                }
	                if (sysComment.getUserId() != null) {
	                    list.add(cb.equal(root.get("userId").as(User.class),sysComment.getUserId()));
	                }
	                if (sysComment.getComment() != null) {
	                    list.add(cb.equal(root.get("comment").as(String.class),sysComment.getComment()));
	                }
	                if (sysComment.getCommentTime() != null) {
	                    list.add(cb.equal(root.get("commentTime").as(BigInteger.class),sysComment.getCommentTime()));
	                }
	                if (sysComment.getIsDelete() != null) {
	                    list.add(cb.equal(root.get("isDelete").as(BigInteger.class),sysComment.getIsDelete()));
	                }
	                if (sysComment.getIsCheck() != null) {
	                    list.add(cb.equal(root.get("isCheck").as(BigInteger.class),sysComment.getIsCheck()));
	                }
	                if (sysComment.getState() != null) {
	                    list.add(cb.equal(root.get("state").as(BigInteger.class),sysComment.getState()));
	                }
	                Predicate[] p = new Predicate[list.size()];
	                return cb.and(list.toArray(p));
	            }
	        },pageable);
	         return result;
	    }

	
}
